package 第三方工具类.Fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.Data;
import org.junit.Test;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Author: SunAo
 * @Date: 2021/5/7 下午5:14
 */
public class FastJson {

    @Test
    public void 泛型数据转化(){
        Person p = new Person();
        p.setName("SunAo");
        p.setPassword("123456");
        String json = JSONObject.toJSONString(p);
        System.out.println("json:"+json);
        Person pp = test(json,Person.class);

        System.out.println(pp.toString());
    }

    public <T> T test(String json,Class<T> clazz){
        return JSON.parseObject(json,new TypeReference<T>(clazz){});
    }

    @Data
    static class PP<T>{
        T p;
    }


    // 将List转为JSON数组字符串
    @Test
    public void lsitToJsonString(){
        System.out.println(JSON.toJSONString(Arrays.asList("1,2,3")));
    }

    @Test
    public void jsonArrayToSplitString(){
        String jsonArray = "['1,2,3']";
        JSONArray array = JSON.parseArray(jsonArray);
        String a = array.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        System.out.println(a);
    }
}
